package com.xxx.mch.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.mch.domain.dto.UserResultScoreDto;
import com.xxx.mch.domain.po.ResultScore;
import com.xxx.mch.domain.po.ResultScoreTemplate;
import com.xxx.mch.domain.request.QueryScoreRequest;
import com.xxx.mch.service.impl.ResultScoreServiceImpl;
import com.xxx.mch.utils.MyPageInfo;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 成果分
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020-02-25
 */
@RestController
@RequestMapping ("resultScores")
public class ResultScoreController
{

    @Autowired
    private ResultScoreServiceImpl resultScoreService;

    @ApiOperation (value = "个人积分明细查询接口")
    @GetMapping (value = "{uid}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<UserResultScoreDto> getList(@PathVariable("uid") String uid, @RequestParam("month") String month)
    {
        UserResultScoreDto scoreDto = resultScoreService.queryUserScore(uid, month);
        return WrapMapper.ok(scoreDto);
    }

    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<MyPageInfo<ResultScore>> getList(@Validated @ModelAttribute QueryScoreRequest queryScoreRequest)
    {
        PageInfo<ResultScore> pageInfo = resultScoreService.queryScoreRecord(queryScoreRequest);
        BigDecimal sumScore = resultScoreService.querySumScore(queryScoreRequest);
        MyPageInfo<ResultScore> myPageInfo = new MyPageInfo<>();
        BeanUtils.copyProperties(pageInfo, myPageInfo);
        myPageInfo.setSumScore(sumScore);
        return WrapMapper.ok(myPageInfo);
    }


    @PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper create(@RequestBody ResultScore resultScore)
    {
        resultScoreService.saveScore(resultScore);
        return WrapMapper.ok();
    }

    /**
     * 获取成果分筛选类型
     * @param queryScoreRequest
     * @return
     */
    @GetMapping (value = "type", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<ResultScore>> resultTypeFilter(@ModelAttribute QueryScoreRequest queryScoreRequest)
    {
        return  WrapMapper.ok(resultScoreService.queryTypeFilter(queryScoreRequest));
    }

    /**
     *
     * @param templateRequest
     * @return
     */
    @PostMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper createOrUpdate(@RequestBody List<ResultScoreTemplate> templateRequest)
    {
        resultScoreService.createOrUpdateTemplate(templateRequest);
        return WrapMapper.ok();
    }

    @GetMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<ResultScoreTemplate>> getTemplate()
    {
        return WrapMapper.ok(resultScoreService.getTemplate());
    }
}
